package cn.edu.xmu.oomall.aftersales.dao;

import cn.edu.xmu.oomall.aftersales.mapper.AftersaleServicePoMapper;
import cn.edu.xmu.oomall.aftersales.model.bo.AfterSale;
import cn.edu.xmu.oomall.aftersales.model.po.AftersaleServicePo;
import cn.edu.xmu.oomall.aftersales.model.po.AftersaleServicePoExample;
import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.*;

/**
 * @author Jiawei Zheng
 * @author zyu
 * @date 2021-12-11
 */
@Repository
public class AfterSaleDao {

    @Autowired
    AftersaleServicePoMapper aftersaleServicePoMapper;

    private static Byte BEDELETED=(byte)1;
    private static Logger logger = LoggerFactory.getLogger(Common.class);

    public ReturnObject insertAfterSale(AfterSale bo){
        try {
            AftersaleServicePo po = cloneVo(bo, AftersaleServicePo.class);
            aftersaleServicePoMapper.insert(po);
            return new ReturnObject(cloneVo(po, AfterSale.class));
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject selectAfterSaleByIdAndUserId(Long userId, Long id){
        try {
            AftersaleServicePo po = aftersaleServicePoMapper.selectByPrimaryKey(id);
            if(po==null){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            if(!po.getCustomerId().equals(userId)){
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            return new ReturnObject(po);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject updateAfterSale(AfterSale bo){
        try{
            AftersaleServicePo po = cloneVo(bo, AftersaleServicePo.class);
            aftersaleServicePoMapper.updateByPrimaryKeySelective(po);
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject selectAfterSaleById(Long id)
    {
        try {
            AftersaleServicePo po = aftersaleServicePoMapper.selectByPrimaryKey(id);
            if(po==null){
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            return new ReturnObject(po);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR, e.getMessage());
        }
    }

    public ReturnObject selectAfterSaleByCondition(AfterSale afterSale, LocalDateTime beginTime,LocalDateTime endTime, Integer page, Integer pageSize)
    {
        try {
            AftersaleServicePoExample example=new AftersaleServicePoExample();
            AftersaleServicePoExample.Criteria criteria=example.createCriteria();
            //顾客只能查询自己的售后单
            if(afterSale.getCustomerId()!=null)
            {
                criteria.andCustomerIdEqualTo(afterSale.getCustomerId());
                //顾客应该查不到逻辑删除的售后单
                criteria.andBeDeletedNotEqualTo(BEDELETED);
            }
            //管理员只能查询对应商铺id的售后单
            else if(afterSale.getShopId()!=null)
            {
                criteria.andShopIdEqualTo(afterSale.getShopId());
                //管理员可以根据售后类型查询
                if(afterSale.getType()!=null)
                {
                    criteria.andTypeEqualTo(afterSale.getType());
                }
            }
            else
            {
                return new ReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            if(afterSale.getState()!=null)
            {
                criteria.andStateEqualTo(afterSale.getState().getCode());
            }
            if(beginTime!=null)
            {
                //售后单的创建时间不少于begintime
                criteria.andGmtCreateGreaterThanOrEqualTo(beginTime);
            }
            if(endTime!=null)
            {
                //售后单的创建时间不晚于结束时间
                criteria.andGmtCreateLessThanOrEqualTo(endTime);
            }
            example.setOrderByClause("`gmt_create` DESC");
            PageHelper.startPage(page,pageSize);
            List<AftersaleServicePo> pos=aftersaleServicePoMapper.selectByExample(example);
            if(pos==null)
            {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            return new ReturnObject(pos);
        }catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
        }
    }

    public ReturnObject selectAfterSaleBySn(String serviceSn) {
        try {
            AftersaleServicePoExample aftersaleServicePoExample = new AftersaleServicePoExample();
            aftersaleServicePoExample.createCriteria().andServiceSnEqualTo(serviceSn);
            List<AftersaleServicePo> aftersaleServicePoList = aftersaleServicePoMapper.selectByExample(aftersaleServicePoExample);
            if (aftersaleServicePoList.isEmpty()) {
                return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
            }
            return new ReturnObject(cloneVo(aftersaleServicePoList.get(0), AfterSale.class));
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR,e.getMessage());
        }
    }
}
